package com.shajj.web.action;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.velocity.context.Context;

import com.caucho.hessian.client.HessianProxyFactory;
import com.shajj.data.DataManager;
import com.shajj.data.LRUCache;
import com.shajj.data.Message;
import com.shajj.data.Record;
import com.shajj.data.RssRecord;
import com.shajj.web.WebAction;

public class IndexAction implements WebAction {

	// private static final String targetURL =
	// "http://shajjcdn.appspot.com/get?type=page";
//	private static final String targetURL = "http://localhost:8888/g";
//	private static final String rssTargetURL = "http://localhost:8888/rss";
	 private static final String targetURL = "http://shajjcdn.appspot.com/g";
	 private static final String rssTargetURL = "http://shajjcdn.appspot.com/rss";

	private static LRUCache<String, List<Message>> cache = new LRUCache<String, List<Message>>();

	private HessianProxyFactory factory = new HessianProxyFactory();

	private DataManager<Record> dataManager;
	private DataManager<RssRecord> rssDataManager;

	@SuppressWarnings("unchecked")
	public IndexAction() {
		try {
			factory.setOverloadEnabled(true);
			dataManager = (DataManager<Record>) factory.create(DataManager.class, targetURL);
			rssDataManager = (DataManager<RssRecord>) factory.create(DataManager.class, rssTargetURL);
		} catch (Exception e) {

		}
	}

	@SuppressWarnings("unchecked")
	public void handleRequest(Context context, HttpServletRequest request) {
		String startString = request.getParameter("start");
		String sizeString = request.getParameter("size");
		String queryString = request.getParameter("s");
		if (queryString == null || queryString.equals("")) {
			int start = 0;
			int size = 20;
			if (startString != null) {
				start = Integer.parseInt(startString);
			}
			if (sizeString != null) {
				size = Integer.parseInt(sizeString);
			}
			String key = start + "_" + size;
			List messages = cache.get(key);
			if (messages == null || messages.size() == 0) {
				messages = getMessageByPage(start, size);
				cache.put(key, messages);
			}
			context.put("messageList", messages);
			context.put("next", start + size);
			context.put("prev", start - size);
			context.put("start", start);
			context.put("size", size);
		} else {
			context.put("messageList", dataManager.search(queryString));
		}

	}

	@SuppressWarnings("unchecked")
	private List getMessageByPage(int start, int size) {
		try {
			List list1 = dataManager.getByCache(start, size);
			List list2 = rssDataManager.getByCache(start, size);
			return mergeList(list1, list2);
		} catch (Exception e) {
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List mergeList(List list1, List list2) {
		if (list1 == null) {
			return list2;
		}
		if (list2 == null) {
			return list1;
		}
		List result = new ArrayList();
		if (list1.size() >= list2.size()) {
			int i = 0;
			for (; i < list2.size(); i++) {
				result.add(list1.get(i));
				result.add(list2.get(i));
			}
			for (int j = i; j < list1.size(); j++) {
				result.add(list1.get(j));
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws MalformedURLException {
		HessianProxyFactory factory = new HessianProxyFactory();
		DataManager dataManager = (DataManager) factory.create(DataManager.class, targetURL);
		List list = dataManager.search("图书馆");
		for (Object r : list) {
			System.out.println(r);
		}
	}

}
